import 'package:flutter/material.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:get/get.dart';

import 'package:ball_lion/entity/my/help/feedback_entity.dart';
import 'package:ball_lion/resources/colours.dart';
import 'package:ball_lion/server/api/user_api.dart';
import 'package:ball_lion/routes/routes.dart';
import 'package:ball_lion/utils/date_time_utils.dart';
import 'package:ball_lion/utils/utils.dart';
import 'package:ball_lion/widgets/loading/loading_widget.dart';
import 'package:ball_lion/widgets/loading/no_data_widget.dart';
import 'package:ball_lion/widgets/base/wqs_text.dart';
import 'package:ball_lion/widgets/ball_appbar.dart';
import 'package:ball_lion/widgets/card.dart';

class FeedbackHistoryPage extends StatefulWidget {
  const FeedbackHistoryPage({super.key});

  @override
  State<FeedbackHistoryPage> createState() => _FeedbackHistoryPageState();
}

class _FeedbackHistoryPageState extends State<FeedbackHistoryPage> {
  List<FeedbackEntity>? history;

  @override
  void initState() {
    getData();
    super.initState();
  }

  Future getData() async {
    history = [];
    history = await UserApi.getFeedbackHistory();
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colours.greyF2F3F7,
        appBar: const BallAppBar(title: "反馈记录"),
        body: SizedBox(
          height: Get.height,
          child: LoadingWidget<bool>(
            isLoading: history == null,
            data: history?.length == 0,
            noData: const NoDataWidget(needScroll: true),
            child: () => SingleChildScrollView(
                child: Column(
                    children: history?.map((e) => item(e)).toList() ?? [])),
          ),
        ));
    ;
  }

  Widget item(FeedbackEntity? entity) {
    return GestureDetector(
      onTap: () => Get.toNamed(Routes.feedbackDetail, arguments: entity),
      child: Block(
          cardStyle: const CardStyle(
            margin: EdgeInsets.all(8),
            padding: EdgeInsets.all(12),
          ),
          child: DefaultTextStyle(
            style: const TextStyle(fontSize: 14, color: Colours.text),
            child: Column(
              children: [
                Row(
                  children: [
                    QsText(
                        DateTime.tryParse(entity?.createTime ?? "")
                                ?.formatedString('yyyy-MM-dd HH:mm') ??
                            "",
                        fontSize: 12,
                        color: Colours.grey99),
                    const Spacer(),
                    QsText(entity?.status == 0 ? "未回复" : "已回复"),
                    Container(width: 6),
                    Image.asset(Utils.getImgPath("arrow_right.png"),
                        width: 6, height: 10)
                  ],
                ).paddingOnly(bottom: 12),
                const Divider(color: Colours.greyEE, height: 0.5),
                textLine("问题类型：", entity?.typeName ?? "")
                    .paddingOnly(bottom: 8, top: 10),
                textLine("问题描述：", entity?.suggest ?? "").paddingOnly(bottom: 8),
                if (entity?.reply.hasValue == true)
                  textLine("处理结果：", entity!.reply!).paddingOnly(bottom: 4)
              ],
            ),
          )),
    );
  }

  Widget textLine(String title, String content) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        QsText(title, color: Colours.grey99),
        Expanded(
            child: !content.contains("<")
                ? QsText(content, maxLines: 4)
                : Html(
                    doNotRenderTheseTags: const {"img"},
                    data: content,
                    style: {
                      "body": Style(
                          margin: Margins.zero, padding: HtmlPaddings.zero),
                      "p": Style(
                          margin: Margins.zero,
                          padding: HtmlPaddings.zero,
                          fontSize: FontSize(14),
                          lineHeight: const LineHeight(1.35),
                          color: Colours.text,
                          maxLines: 4,
                          textOverflow: TextOverflow.ellipsis)
                    },
                  ))
      ],
    );
  }
}
